package com.sfzd5.UI;

import com.sfzd5.exam.helplibrary.Generator;
import com.sfzd5.exam.helplibrary.taskstatisitcs.QuestionInfo;
import com.sfzd5.exam.helplibrary.taskstatisitcs.TaskStatistics;
import com.sfzd5.html.MyExtendedHtmlEditorKit;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import javax.swing.text.StyleContext;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.util.Locale;

public class QuestionInfoPanel {
    public JPanel mPanel;
    private JTextPane htmlPane;
    private JLabel lb_difficult;
    private JLabel lb_rightRate;
    private JLabel lb_process;
    private JButton 讲解Button;

    private QuestionInfo questionInfo;
    private TaskStatistics taskStatistics;
    NumberFormat format;

    public void setQuestionClickListener(QuestionClickListener questionClickListener) {
        this.questionClickListener = questionClickListener;
    }

    private QuestionClickListener questionClickListener;

    public QuestionInfoPanel(TaskStatistics taskStatistics, long questionId) {
        this.format = NumberFormat.getPercentInstance();
        this.format.setMaximumFractionDigits(2);
        this.taskStatistics = taskStatistics;
        this.questionInfo = taskStatistics.questionInfoHashMap.get(questionId);

        htmlPane.setText(generateQuestionHtml(Generator.generateQuestionHtml_forPanelOnlyQuestion(questionInfo.q)));
        htmlPane.setMaximumSize(new Dimension(700, 1000));
        htmlPane.setMinimumSize(new Dimension(700, 160));

        resetInfo();
        讲解Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (questionClickListener != null) {
                    questionClickListener.QuestionClick(questionInfo);
                }
            }
        });
    }

    public void resetInfo() {
        lb_process.setText(String.format("%d/%d", questionInfo.overCount, taskStatistics.stuInfoList.size()));
        if (questionInfo.q.questiontype.contains("选"))
            lb_rightRate.setText(format.format(questionInfo.getRate()));
        else
            lb_rightRate.setText("--");
        lb_difficult.setText(hard(questionInfo.difficult));
    }

    // 不再使用了，今后会删除
    public QuestionInfoPanel(QuestionInfo questionInfo, int studentCount, NumberFormat format) {
        this.questionInfo = questionInfo;
        this.format = format;

        //htmlPane.setEditorKit(new MyExtendedHtmlEditorKit());


    }

    public String generateQuestionHtml(String html) {
        String width = "650px";
        String fontSzie = "24px";
        StringBuilder sb = new StringBuilder();
        sb.append("<!DOCTYPE html><html><head><meta charset=\"utf-8\" /><style type=\"text/css\"> .question{ margin:0 auto; width:")
                .append(width)
                .append("; font-size:")
                .append(fontSzie)
                .append("; text-align:left; } </style> </head> <body> <div class=\"question\">")
                .append(html)
                .append("</div></body></html>");
        return sb.toString();
    }

    static String[] hardArray = new String[]{
            "☆☆☆☆☆",
            "★☆☆☆☆",
            "★★☆☆☆",
            "★★★☆☆",
            "★★★★☆",
            "★★★★★"
    };

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        mPanel = new JPanel();
        mPanel.setLayout(new BorderLayout(0, 0));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(1, 8, new Insets(4, 4, 4, 4), -1, -1));
        panel1.setBackground(new Color(-4145088));
        mPanel.add(panel1, BorderLayout.SOUTH);
        final JLabel label1 = new JLabel();
        Font label1Font = this.$$$getFont$$$(null, -1, 16, label1.getFont());
        if (label1Font != null) label1.setFont(label1Font);
        label1.setForeground(new Color(-16777216));
        label1.setText("  进度：");
        panel1.add(label1, new com.intellij.uiDesigner.core.GridConstraints(0, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final com.intellij.uiDesigner.core.Spacer spacer1 = new com.intellij.uiDesigner.core.Spacer();
        panel1.add(spacer1, new com.intellij.uiDesigner.core.GridConstraints(0, 6, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
        lb_process = new JLabel();
        Font lb_processFont = this.$$$getFont$$$(null, -1, 16, lb_process.getFont());
        if (lb_processFont != null) lb_process.setFont(lb_processFont);
        lb_process.setForeground(new Color(-16777216));
        lb_process.setText("0/5");
        panel1.add(lb_process, new com.intellij.uiDesigner.core.GridConstraints(0, 1, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JLabel label2 = new JLabel();
        Font label2Font = this.$$$getFont$$$(null, -1, 16, label2.getFont());
        if (label2Font != null) label2.setFont(label2Font);
        label2.setForeground(new Color(-16777216));
        label2.setText("        正确率：");
        panel1.add(label2, new com.intellij.uiDesigner.core.GridConstraints(0, 2, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        lb_rightRate = new JLabel();
        Font lb_rightRateFont = this.$$$getFont$$$(null, -1, 16, lb_rightRate.getFont());
        if (lb_rightRateFont != null) lb_rightRate.setFont(lb_rightRateFont);
        lb_rightRate.setForeground(new Color(-16777216));
        lb_rightRate.setText("50%");
        panel1.add(lb_rightRate, new com.intellij.uiDesigner.core.GridConstraints(0, 3, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JLabel label3 = new JLabel();
        Font label3Font = this.$$$getFont$$$(null, -1, 16, label3.getFont());
        if (label3Font != null) label3.setFont(label3Font);
        label3.setForeground(new Color(-16777216));
        label3.setText("        难度：");
        panel1.add(label3, new com.intellij.uiDesigner.core.GridConstraints(0, 4, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        lb_difficult = new JLabel();
        Font lb_difficultFont = this.$$$getFont$$$(null, -1, 16, lb_difficult.getFont());
        if (lb_difficultFont != null) lb_difficult.setFont(lb_difficultFont);
        lb_difficult.setForeground(new Color(-16777216));
        lb_difficult.setText("Label");
        panel1.add(lb_difficult, new com.intellij.uiDesigner.core.GridConstraints(0, 5, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        讲解Button = new JButton();
        讲解Button.setText("讲解");
        panel1.add(讲解Button, new com.intellij.uiDesigner.core.GridConstraints(0, 7, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        htmlPane = new JTextPane();
        htmlPane.setContentType("text/html");
        htmlPane.setEditable(false);
        htmlPane.setEnabled(true);
        htmlPane.setMargin(new Insets(16, 3, 3, 3));
        htmlPane.setMaximumSize(new Dimension(950, 1000));
        htmlPane.setMinimumSize(new Dimension(950, 100));
        mPanel.add(htmlPane, BorderLayout.CENTER);
    }

    /**
     * @noinspection ALL
     */
    private Font $$$getFont$$$(String fontName, int style, int size, Font currentFont) {
        if (currentFont == null) return null;
        String resultName;
        if (fontName == null) {
            resultName = currentFont.getName();
        } else {
            Font testFont = new Font(fontName, Font.PLAIN, 10);
            if (testFont.canDisplay('a') && testFont.canDisplay('1')) {
                resultName = fontName;
            } else {
                resultName = currentFont.getName();
            }
        }
        Font font = new Font(resultName, style >= 0 ? style : currentFont.getStyle(), size >= 0 ? size : currentFont.getSize());
        boolean isMac = System.getProperty("os.name", "").toLowerCase(Locale.ENGLISH).startsWith("mac");
        Font fontWithFallback = isMac ? new Font(font.getFamily(), font.getStyle(), font.getSize()) : new StyleContext().getFont(font.getFamily(), font.getStyle(), font.getSize());
        return fontWithFallback instanceof FontUIResource ? fontWithFallback : new FontUIResource(fontWithFallback);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return mPanel;
    }

    enum Difficult {
        容易,
        较易,
        普通,
        较难,
        困难
    }

    public static String hard(String hard) {
        return hardArray[Difficult.valueOf(hard).ordinal()];
    }
}
